$(function () {
    $("#page").load("/back/page.html");
    findAll();
    // $("#page").load("/html/page.html");
})

function findAll() {

    let data = {
        pageNum: $.trim($("#pageNum").val()),
        pageSize: $.trim($("#pageSize").val())
    };
    $.ajax({
        url: '/back/product/findAll',
        data: data,
        type: 'get',
        dataType: "json",
        success: function (data) {
            setData(data.list);
            $("#pageCount").html(data.number);
        }
    })
}

function setData(data) {
    let html = '';
    for (let i = 0; i < data.length; i++) {
        html += '<tr>\n' +
            '        <td>' + data[i].id + '</td>\n' +
            '        <td>' + data[i].title + '</td>\n' +
            '        <td><img src="' + data[i].imgHref + '" width="20px" height="20px"></td>\n' +
            '        <td>' + data[i].content + '</td>\n' +
            '        <td>' + data[i].cuisineName + '</td>\n' +
            '        <td><a href="javascript:edit(' + data[i].id + ')"> 编辑</a> <a href="javascript:del(' + data[i].id + ')">删除</a></td>\n' +
            '    </tr>';
    }
    $("#productFindAll").html(html);
}

function edit(id) {
    localStorage.setItem("productId", id);
    // localStorage.setItem("productTitle",title);
    //点击编辑跳转到编辑页面，同时，需要根据当前id将所有数据赋值到对应的地方
    $("#right").load("/back/product/productUpdate.html");
}

function del(id) {
    $.ajax({
        url: '/back/product/del',
        data: {id: id},
        type: 'get',
        dataType: "json",
        success: function (data) {
            if (data.number == 1) {
                findAll();
            } else {
                alert("删除失败")
            }
        }
    })
}

function firstAll() {
    $("#pageNum").val(1);
    findAll();
}

function lastAll() {
    let num = $.trim($("#pageNum").val());

    let pageNum = num == null || num == "" ? 1 : parseInt(num);
    // if (pageNum < 1) {
    //     pageNum = 1;
    // }
    $("#pageNum").val(--pageNum);
    findAll();
}

function nextAll() {
    let num = $.trim($("#pageNum").val());
    let pageNum = num == null || num == "" ? 1 : parseInt(num);

    //取得总条数
    let count = $("#pageCount").html();
    let pageCount = count == null || count == "" ? 1 : parseInt(count);
    //取得每一页显示的条数
    let size = $.trim($("#pageSize").val());
    let pageSize = size == null || size == "" ? 1 : parseInt(size);
    //算出总页数
    let endPage = Math.ceil(pageCount / pageSize);
    if (pageNum != endPage) {
        ++pageNum;
    }
    $("#pageNum").val(pageNum);
    findAll();
}

function endAll() {
    let count = $("#pageCount").html();
    let pageCount = count == null || count == "" ? 1 : parseInt(count);
    let size = $.trim($("#pageSize").val());
    let pageSize = size == null || size == "" ? 1 : parseInt(size);
    let pageNum = Math.ceil(pageCount / pageSize);
    // if (pageCount % pageSize == 0) {
    //     pageNum = pageCount / pageSize;
    // } else {
    //     pageNum = parseInt(pageCount / pageSize)  + 1;
    // }
    $("#pageNum").val(pageNum);
    findAll();
}
